package com.asia5b.a5mall.middleware.api

import android.annotation.SuppressLint
import com.asia5b.a5mall.middleware.base.BaseApplication
import com.asia5b.a5mall.middleware.utils.UUID
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   API管理器
 */
object ApiManager {

    /**
     * @Name  apiFectory
     * @Type  NetFectory
     * @Note  API构建者
     */
    @SuppressLint("StaticFieldLeak")
    private val apiFectory = NetFectory.build(BaseApplication.sApplication)

    /**
     * @Name  com.asia5b.a5mall.middleware.api.ApiManager.Companion.BASE_URL
     * @Type  UrlManager
     * @Note  服务器URL控制器
     */
    private val urlManager = UrlManager.build()

    /** 全局分页条数 */
    val PAGE_SIZE = 10

    /**
     * @Name  com.asia5b.a5mal.middleware.api.ApiManager.EXTRA_API
     * @Type  ExtraApi
     * @Note  其他API
     */
    val API_EXTRA: ExtraApi by lazy { apiFectory.createService(ExtraApi::class.java, urlManager.buyer) }


    /**
     * @Name  com.asia5b.a5mal.middleware.api.ApiManager.BASE_URL
     * @Type  BaseApi
     * @Note  基础API
     */
    val API_BASE: BaseApi by lazy { apiFectory.createService(BaseApi::class.java, urlManager.basic) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_AFTERSALE
     * @Type  AfterSaleApi
     * @Note  售后API
     */
    val API_AFTERSALE: AfterSaleApi by lazy { apiFectory.createService(AfterSaleApi::class.java, urlManager.buyer) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_CART
     * @Type  CartApi
     * @Note  购物车API
     */
    val API_CART: CartApi by lazy { apiFectory.createService(CartApi::class.java, urlManager.buyer) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_CATEGORY
     * @Type  CategoryApi
     * @Note  分类API
     */
    val API_CATEGORY: CategoryApi by lazy { apiFectory.createService(CategoryApi::class.java, urlManager.buyer) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_GOODS
     * @Type  GoodsApi
     * @Note  商品API
     */
    val API_GOODS: GoodsApi by lazy { apiFectory.createService(GoodsApi::class.java, urlManager.buyer) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_MEMBER
     * @Type  MemberApi
     * @Note  会员API
     */
    val API_MEMBER: MemberApi by lazy { apiFectory.createService(MemberApi::class.java, urlManager.buyer) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_ORDER
     * @Type  OrderApi
     * @Note  订单API
     */
    val API_ORDER: OrderApi by lazy { apiFectory.createService(OrderApi::class.java, urlManager.buyer) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_PAYMENT
     * @Type  PaymentApi
     * @Note  支付API
     */
    val API_PAYMENT: PaymentApi by lazy { apiFectory.createService(PaymentApi::class.java, urlManager.buyer) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_PROMOTION
     * @Type  PromotionApi
     * @Note  促销API
     */
    val API_PROMOTION: PromotionApi by lazy { apiFectory.createService(PromotionApi::class.java, urlManager.buyer) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_SHOP
     * @Type  ShopApi
     * @Note  店铺API
     */
    val API_SHOP: ShopApi by lazy { apiFectory.createService(ShopApi::class.java, urlManager.buyer) }

    /**
     * @Name  com.enation.javashop.android.lib.api.ApiManager.Companion.API_TRADE
     * @Type  TradeApi
     * @Note  交易API
     */
    val API_TRADE: TradeApi by lazy { apiFectory.createService(TradeApi::class.java, urlManager.buyer) }


    /**
     * @Name  PASSPORT_API
     * @Type  PassportApi
     * @Note  护照
     */
    val API_PASSPORT: PassportApi by lazy { apiFectory.createService(PassportApi::class.java, urlManager.buyer) }


    /**
     * @Note   刷新Token
     * @param  refrehToken 刷新Token
     */
    fun refreshToken(refrehToken: String): Response {
        val mediaType = "text/x-markdown; charset=utf-8".toMediaTypeOrNull()
        val requestBody = "refrehToken"
        val okHttpClient = OkHttpClient()
        val getTokenRequest = Request.Builder()
                .url("${urlManager.buyer}passport/token?refersh_token=$refrehToken")
                .post(RequestBody.create(mediaType, requestBody))
                .addHeader("uuid", UUID.uuid)
                .build()
        val call = okHttpClient.newCall(getTokenRequest)
        return call.execute()
    }
}
